<?php

namespace app\admin\controller;

use think\Request;
use app\admin\service\GoodsService;

class Goods extends Base
{
    /**
     * 商品列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $goodsService = new GoodsService;
        //获取所有商品分类
        $goodsCates = $goodsService->getAllCates();
        //处理搜索条件
        $param = Request::instance()->param();
        $where = $goodsService->indexCondition($param);
        // dump($where);die;
        //获取商品
        $data = $goodsService->index($where,$param);
        $this->assign([
            'goodsCates'  => $goodsCates,
            'where'       => $where['data'],
            'data'        => $data,
        ]);
        return $this->fetch();
    }

    /**
     * 审核商品
    */
    public function examine()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $goodsService = new GoodsService;
            return json($goodsService->examine($post));
        }
    }

    /**
     * 拒绝商品
    */
    public function refuse()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $goodsService = new GoodsService;
            return json($goodsService->refuse($post));
        }
    }

    /**
     * 分类列表
    */
    public function cates()
    {
        $param = Request::instance()->param();
        $goodsService = new GoodsService;
        $data = $goodsService->cates($param);
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 添加分类
    */
    public function addCate()
    {
        if(Request::instance()->isGet()){
            return $this->fetch();
        }else{
            $post = Request::instance()->post();
            $goodsService = new GoodsService;
            $data = $goodsService->addCatePost($post);
            return json($data);
        }
    }

    /**
     * 编辑分类
    */
    public function editCate()
    {
        $goodsService = new GoodsService;
        if(Request::instance()->isGet()){
            $id = input('id');
            $data = $goodsService->editCateget($id);
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $post = Request::instance()->post();
            return json($goodsService->editCatePost($post));
        }
    }
}
